package org.gvp.generator.core.writer.impl;

import org.gvp.generator.core.WriterToResources;
import org.gvp.generator.core.writer.AbstractFileWriter;
import org.gvp.generator.props.CreateProps;
import org.gvp.generator.props.ProjectProps;
import org.gvp.generator.table.TableInfo;

import java.nio.file.Path;
import java.nio.file.Paths;

@WriterToResources
public class TsReduxFileWriter extends AbstractFileWriter {
    @Override
    public void execute(TableInfo tableInfo, ProjectProps project) {
        String upName = tableInfo.getUpperName();
        String rem = tableInfo.getRemark();

        String stateTypeDef = """
                /**
                 * %s状态管理类型
                 */
                export type %sState = StatePagingType<%s>
                """.formatted(tableInfo.getRemark(),upName,upName);
        String imps = """
                import { createSlice, PayloadAction} from '@reduxjs/toolkit';
                                
                import { StatePagingType } from 'storage/redux';
                import RequestStatus from 'storage/redux/RequestStatus';
                import {builderPagingHandler} from "storage/redux/reducer";
                import {fetchAdd%s, fetchDelete%s, fetchUpdate%s, fetch%sPageData}\s
                    from "storage/redux/async-action/%sAsyncAction";
                import UserStorage from "storage/local/UserStorage";
                
                import {%s} from "type/entity/%sUser";
                """.formatted(upName,upName,upName,upName,tableInfo.getLowerName(),upName,upName);
        String initState = """
                /** 状态初始化数据 */
                const initialState: %s = {
                    data: [],
                    loading: false,
                    requestStatus: RequestStatus.INIT,
                    currentPage: 1,
                    pageSize: UserStorage.getInstance().getAdminPageConfigFromLocalStore().%sPageSize,
                    total: 0,
                    totalPage: 10
                };
                """.formatted(upName,tableInfo.getLowerName());

        String slice = """
                export const %sSlice = createSlice({
                    name: '%s',
                    initialState,
                    reducers: {
                    },
                    extraReducers: (builder) => {
                        builderPagingHandler(builder, fetch%sPageData);
                        /** 添加%s信息 */
                        builder.addCase(fetchAdd%s.fulfilled, (state, action) => {
                            state.requestStatus = RequestStatus.REFRESH;
                        });
                        /** 修改%s信息 */
                        builder.addCase(fetchUpdate%s.fulfilled, (state, action) => {
                            state.requestStatus = RequestStatus.REFRESH;
                        });
                        /** %s用户信息 */
                        builder.addCase(fetchDelete%s.fulfilled, (state, action) => {
                            state.requestStatus = RequestStatus.REFRESH;
                        });
                    }
                });
                """.formatted(tableInfo.getLowerName(),tableInfo.getLowerName(),upName,rem,upName,rem,upName,rem,upName);
        channel.writeNewline(imps);
        channel.writeNewline(stateTypeDef);
        channel.writeNewline(initState);
        channel.writeNewline(slice);
        channel.writeNewline("""
                export const {} = %sSlice.actions;
                export default %sSlice.reducer;
                """.formatted(tableInfo.getLowerName(),tableInfo.getLowerName()));
    }

    @Override
    public boolean isWriterFile(CreateProps create) {
        return create.isCreateTsRedux();
    }

    @Override
    public Path getFilePath(String basePath, ProjectProps project, String fileName) {
        return Paths.get(basePath,project.getTypescriptDirPath(),fileName + "_redux.tsx");
    }
}
